<template>
  <van-button
    v-if="isFollowed"
    class="follow-bth"
    round
    size="small"
    :loading="followLoading"
    @click="onFollow"
  >
    已关注
  </van-button>
  <van-button
    v-else
    class="follow-btn"
    type="info"
    round
    color="#3296fa"
    size="small"
    icon="plus"
    :loading="followLoading"
    @click="onFollow"
  >
    关注
  </van-button>
</template>

<script>
import { addFollow, deleteFollow } from "@/api/user";

export default {
  name: "FollowUser",
  props: {
    isFollowed: {
      type: Boolean,
      required: true,
    },
    articleId: {
      type: [String, Number, Object],
      required: true,
    },
  },
  model: {
    prop: 'isFollowed',
    event: 'update-is_followed'
  },
  data() {
    return {
      followLoading: false, // 关注按钮loading属性
    };
  },
  methods: {
    async onFollow() {
      try {
        this.followLoading = true;
        if (!this.isFollowed) {
          await addFollow(this.articleId);
        } else {
          await deleteFollow(this.articleId);
        }
      } catch (err) {
        let message = "操作失败，请重试！";
        if (err.response && err.response.status === 400) {
          message = "你不能关注你自己";
        }
        this.$toast.fail(message);
      }
      this.followLoading = false;
      this.$emit("update-is_followed", !this.isFollowed);
    },
  },
};
</script>

<style>
</style>